﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;

namespace IQReportes_Batch.WebService
{
    class ConsumeRestGeneric<T>
    {
        public T consume(string url)
        {
            var instance = Activator.CreateInstance<T>();
            var cliente = new WebClient();
            cliente.Encoding = System.Text.Encoding.UTF8;
            var content = cliente.DownloadString(url);
            if (content.Equals("")) return default(T);
            DataContractJsonSerializer deserializer = new DataContractJsonSerializer(instance.GetType());
            using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(content)))
            {
                return (T)deserializer.ReadObject(ms);
            }
        }
    }
}
